<?php

namespace App\Admin\Actions\Grid\User;

use App\Models\User\WxUser;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\BatchAction;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class UserGenderManAction extends BatchAction
{
    /**
     * @return string
     */
	protected $title = '一键修改性别-男';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        $ids = $this->getKey();
        DB::transaction(function () use ($ids){
            foreach ($ids as $id){
                WxUser::where('id', $id)->update(['gender'=>1]);
                Cache::forget('get:cached:user:'.$id);
            }
        });

        return $this->response()
            ->success('操作成功'.json_encode($ids))
            ->refresh();
    }

    /**
	 * @return string|array|void
	 */
	public function confirm()
	{
		 return ['确认修改选中用户的性别为男?'];
	}

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [];
    }
}
